from django import forms

"""
这两个类是实现前端显示输入控件的样式与Bootstrap保持一致
"""

class Bootstrap:
    """
    定义Bootstrap基类，让所有表单控件的class都继承这个类，减少代码量
    """

    exclude_filed = []  # 过滤字段不让引用Bootstrap样式

    # 给所有表单控件添加属性
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            # 判断要过滤掉的字段
            if name in self.exclude_filed:
                continue
            # 如果表单控件中本来就有值，就追加属性
            if field.widget.attrs:
                field.widget.attrs["class"] = "form-control"
            else:
                field.widget.attrs = {"class": "form-control"}

class BootstrapModelForm(Bootstrap, forms.ModelForm):
    pass

class BootstrapForm(Bootstrap, forms.Form):
    pass
